﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;


namespace StreetSmartAdmin
{
    public partial class GetFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Get the file id from the query string
            int id = Convert.ToInt16(Request.QueryString["ID"]);


            DataTable file = new DataTable();
            using (SqlConnection connection = new SqlConnection())
            {

                string connectionString = null;
                System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];

                connectionString = connString.ConnectionString;
                System.Data.SqlClient.SqlConnection connectionsql = null;

                connectionsql = new System.Data.SqlClient.SqlConnection(connectionString);
                connectionsql.Open();
                
                //OpenConnection(connection);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connectionsql;
                cmd.CommandTimeout = 0;

                cmd.CommandText = "SELECT ID, NAME, SIZE, DATA FROM tblFeedback_Case_File "
                    + "WHERE ID=@ID";
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter adapter = new SqlDataAdapter();

                cmd.Parameters.Add("@ID", SqlDbType.Int);
                cmd.Parameters["@ID"].Value = id;

                adapter.SelectCommand = cmd;
                
                adapter.Fill(file);
                
                connection.Close();



            }
            // Get the file from the database
            //DataTable file = FileUtilities.GetAFile(id);
            DataRow row = file.Rows[0];

            string name = (string)row["NAME"];
            //string contentType = (string)row["ContentType"];
            Byte[] data = (Byte[])row["DATA"];

            // Send the file to the browser
            //Response.AddHeader("Content-type", contentType);
            Response.AddHeader("Content-Disposition", "attachment; filename=" + name);
            Response.BinaryWrite(data);
            Response.Flush();
            Response.End();

        }
    }
}